package com.neusoft.elm.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.neusoft.elm.dao.FoodDao;
import com.neusoft.elm.po.Business;
import com.neusoft.elm.po.Food;
import com.neusoft.elm.util.DBUtil;

public class FoodDaoImpl implements FoodDao{

	private Connection con =null;
	private ResultSet  rs =null;
	private PreparedStatement pst =null;
	
	@Override
	public List<Food> getFoodlist(Integer BusinessId) {
		List<Food> list = new ArrayList<>();
		String sql = "select * from food where businessId = ? ";
		try {				 													 //导入查询到的商家信息
			con =DBUtil.getConnection();
			pst =con.prepareStatement(sql);
			pst.setInt(1,BusinessId);
			rs = pst.executeQuery();
			while(rs.next()) {
				Food food = new Food();
				food.setFoodID(rs.getInt("FoodID"));
				food.setFoodName(rs.getString("FoodName"));
				food.setFoodExplain(rs.getString("FoodExplain"));
				food.setFoodPrice(rs.getDouble("FoodPrice"));
				food.setBusinessID(rs.getInt("BusinessID"));
				list.add(food);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {               //释放资源
			DBUtil.close(rs, pst, con);
		}
		return list;
	}
	@Override
	public int postFood(Food food) {
		int result = 0;
		String sql = "insert into Food value(null,?,?,?,?)";
		try {				 										 //导入新建立的商家信息
			con =DBUtil.getConnection();

			pst =con.prepareStatement(sql);
			pst.setString(1, food.getFoodName());
			pst.setString(2, food.getFoodExplain());
			pst.setDouble(3, food.getFoodPrice());
			pst.setInt(4, food.getBusinessID());

			result = pst.executeUpdate();
			
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} finally {               //释放资源
				DBUtil.close(null, pst, con);
			}
		return result;
	}
	@Override
	public Food getFood_Id(Integer FoodId) {
		Food food = null;
		String sql = "select * from food where FoodId = ? ";
		try {				 													 //导入查询到的商家信息
			con =DBUtil.getConnection();
			pst =con.prepareStatement(sql);
			pst.setInt(1,FoodId);
			rs = pst.executeQuery();
			while(rs.next()) {
				food = new Food();
				food.setFoodID(rs.getInt("FoodID"));
				food.setFoodName(rs.getString("FoodName"));
				food.setFoodExplain(rs.getString("FoodExplain"));
				food.setFoodPrice(rs.getDouble("FoodPrice"));
				food.setBusinessID(rs.getInt("BusinessID"));

			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {               //释放资源
			DBUtil.close(rs, pst, con);
		}
		return food;
	}
	@Override
	public int putFood(Food food) {
		int result = 0;
		String sql = "update food set FoodName=?,FoodExplain=?,FoodPrice=? where FoodID=?";
		try {				 													 //导入查询到的商家信息
			con =DBUtil.getConnection();
			pst =con.prepareStatement(sql);
			pst.setString(1,food.getFoodName());
			pst.setString(2,food.getFoodExplain());
			pst.setDouble(3,food.getFoodPrice());
			pst.setInt(4,food.getFoodID());

			result = pst.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {               //释放资源
			DBUtil.close(null, pst, con);
		}
		return result;
	}
	@Override
	public int deleteFood(Integer FoodId) {
		int result = 0;
		String sql = "delete from food where FoodID=?";
		try {				 													 //导入查询到的商家信息
			con =DBUtil.getConnection();
			pst =con.prepareStatement(sql);
			pst.setInt(1,FoodId);

			result = pst.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {               //释放资源
			DBUtil.close(null, pst, con);
		}
		return result;
	}
	
	
}
